package tongcheng.com.tongcheng.widght;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.Marker;
import com.luck.picture.lib.config.PictureConfig;

import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.bean.MapDataBean_DailiShang;
import tongcheng.com.tongcheng.utils.GlideApp;


/**
 * Created by Administrator on 2016/9/27.
 */
public class MapTopPopWindow_other {

    private  MapDataBean_DailiShang.DataBean mDatas;
    private PopupWindow popupWindow;
    private Context mContext;
    private Activity mActivity;
    public int maxChooseNum = 1;
    public int minChooseNum = 1;
  public   int single = PictureConfig.SINGLE;
    private String[] data = new String[]{"相册", "拍照"};
    public Marker marker;

    public MapTopPopWindow_other(Context context, Activity activity, Marker marker) {
        this.mContext = context;
        this.mActivity = activity;
        this.marker = marker;
        mDatas = (MapDataBean_DailiShang.DataBean) marker.getObject();
        init();
        initListener();

    }




    private void initListener() {
        //popWindow退出后 模糊背景取消
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
              /*  WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                lp.alpha = 1f;
                mActivity.getWindow().setAttributes(lp);*/
                marker.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                        .decodeResource(mContext.getResources(),
                                R.mipmap.bashou)));
            }
        });

    }

    private void init() {
        //3个参数的,填写视图,宽度,高度,
        //4个参数的:最后一个是是否获取焦点,true表示获取焦点
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_map_top_window_other, null);
        //设置详细信息的点击回调
        view.findViewById(R.id.map_pop_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onBackLitener != null) {
                    onBackLitener.onBackClick(mDatas);
                }
            }
        });

        TextView map_pop_title = view.findViewById(R.id.map_pop_title);
        map_pop_title.setText(mDatas.address);//设置地址
        TextView map_pop_name = view.findViewById(R.id.map_pop_name);
        map_pop_name.setText(mDatas.user_name);//设置公司名字

        ImageView map_pop_icon1 = view.findViewById(R.id.map_pop_icon1);
        ImageView map_pop_icon2 = view.findViewById(R.id.map_pop_icon2);
        ImageView map_pop_icon3 = view.findViewById(R.id.map_pop_icon3);


        if (!TextUtils.isEmpty(mDatas.documents_features)) {
            String[] split = mDatas.documents_features.split(",");
            for (int i = 0; i < split.length; i++) {
                String s = ConStans.BaseUrl_Test_url + split[i];
                if (i == 0) {
                    GlideApp.with(mActivity).load(s).error(R.mipmap.moren).placeholder(R.mipmap.moren).into(map_pop_icon1);
                } else if (i == 1) {
                    GlideApp.with(mActivity).load(s).error(R.mipmap.moren).placeholder(R.mipmap.moren).into(map_pop_icon2);
                } else if (i == 2) {
                    GlideApp.with(mActivity).load(s).error(R.mipmap.moren).placeholder(R.mipmap.moren).into(map_pop_icon3);
                }
            }
        }



        //指定的popWindow显示
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //在popupWindow之外的区域可以触摸
        popupWindow.setOutsideTouchable(true);
        //设置 弹出后多余的模糊背景
       /* WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = 0.5f;
        mActivity.getWindow().setAttributes(lp);*/
        //设置背景色(Drawable) ,颜色?
        Drawable drawable = new ColorDrawable(Color.alpha(R.color.white)); //透明色
        popupWindow.setBackgroundDrawable(drawable);



    }

    public void show(LinearLayout view) {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
        popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public void show(View view) {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
        popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public void show(RelativeLayout view) {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
        //popupWindow.showAtLocation(view, Gravity.TOP, 0, mContext.getResources().getDimension(R.dimen.));
        popupWindow.showAsDropDown(view);
    }

    /**
     * ItemClick的回调接口
     *
     * @author zhy
     */
    public interface OnBackLitener {
        void onBackClick(MapDataBean_DailiShang.DataBean item);
    }

    private OnBackLitener onBackLitener;

    public void setOnItemClickLitener(OnBackLitener onBackLitener) {
        this.onBackLitener = onBackLitener;
    }
}
